package mashuaiproject.mashuaiproject.controller;
import mashuaiproject.mashuaiproject.dto.CommentListDto;
import mashuaiproject.mashuaiproject.dto.QuesionUserDto;
import mashuaiproject.mashuaiproject.dto.TagDto;
import mashuaiproject.mashuaiproject.exption.CustomizeCode;
import mashuaiproject.mashuaiproject.exption.CustomizeException;
import mashuaiproject.mashuaiproject.mapper.AttributeMappper;
import mashuaiproject.mashuaiproject.mapper.QuesionMapper;
import mashuaiproject.mashuaiproject.mapper.SaveQuesionMapper;
import mashuaiproject.mashuaiproject.model.Quesions;
import mashuaiproject.mashuaiproject.model.Save;
import mashuaiproject.mashuaiproject.model.Users;
import mashuaiproject.mashuaiproject.serivce.JMontiorSerivce;
import mashuaiproject.mashuaiproject.serivce.QuesionUserSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class PublishController {
    @Autowired
    private QuesionMapper quesionMapper;
    @Autowired
    private QuesionUserSerivce quesionUserSerivce;
    @Autowired
    private SaveQuesionMapper saveQuesionMapper;
    @Autowired
    private AttributeMappper attributeMappper;
    @Autowired
    private JMontiorSerivce jMontiorSerivce;
    //点击边框文章携带文章的id
    @GetMapping("/publish/{id}")
    public String editQuesion(@PathVariable("id") Integer id,
                  Model model){
        Quesions quesions=quesionMapper.getByidQuesion(id);
        model.addAttribute("title",quesions.getTitle());
        model.addAttribute("description",quesions.getDescription());
        model.addAttribute("tag",quesions.getTag());
        model.addAttribute("quesionId",quesions.getId());//携带文章的id利用隐藏于传递值
        model.addAttribute("frameworkList", TagDto.frameworkList());
        model.addAttribute("servetList", TagDto.servetList());
        model.addAttribute("toolList", TagDto.toolList());
        model.addAttribute("progrmList", TagDto.progrmList());
        return "publish";
    }



    @GetMapping("/publish")
    public String publish(Model model){
        //携带标签
        model.addAttribute("frameworkList", TagDto.frameworkList());
        model.addAttribute("servetList", TagDto.servetList());
        model.addAttribute("toolList", TagDto.toolList());
        model.addAttribute("progrmList", TagDto.progrmList());
        return "publish";
    }

    @PostMapping("/publish")
    public String doPostPublish(
            @RequestParam("title") String title
             , @RequestParam("description") String  description
             , @RequestParam("tag") String  tag
            ,@RequestParam(value = "quesionId",required = false) Integer quesionId
             , Model model
            , HttpServletRequest request
            ){
        //required = false代表正好参数可以不穿对，默认为true
        model.addAttribute("frameworkList", TagDto.frameworkList());
        model.addAttribute("servetList", TagDto.servetList());
        model.addAttribute("toolList", TagDto.toolList());
        model.addAttribute("progrmList", TagDto.progrmList());
        //不管成不成功都把用户填写的写信携带过去
        model.addAttribute("title",title);
        model.addAttribute("description",description);
        model.addAttribute("tag",tag);
        if(title==null || title==""){
            model.addAttribute("errors","标题为空");
            return "publish";
        }
        if(description==null || description==""){
            model.addAttribute("errors","描述为空");
            return "publish";
        }
        if(tag==null || tag==""){
            model.addAttribute("errors","标签最少写一个");
            return "publish";

        }

        Users users= (Users) request.getSession().getAttribute("user");
        if(users==null){
            model.addAttribute("errors","用户未登录");
            return "publish";
        }

        Quesions quesions=new Quesions();
        quesions.setDescription(description);
        quesions.setTag(tag);
        quesions.setTitle(title);
        quesions.setCreator(users.getId());
        quesions.setGmt_create(System.currentTimeMillis());
        quesions.setGmt_modified(quesions.getGmt_create());

        Users user = (Users) request.getSession().getAttribute("user");
        //说明是发帖
        if(quesionId==null){
            quesionMapper.creator_publish(quesions);
            Integer judge=jMontiorSerivce.checkHasFristFatie(user.getId(),"fatie");
            //更新等级
            jMontiorSerivce.Upleave(user.getId());
            //插入动态：
            Object object=title;
            Integer insert =jMontiorSerivce.insertDongtai(users.getId(),1,object);//用户id，动态类型，不集体类型

        }

        //然后判断quesionId是否为空，不为空就是更新操作
        if(quesionId!=null){
          //编辑文字成功之后直接返回上一页
            quesions.setId(quesionId);
            int updateQuesion=quesionMapper.updateQueions(quesions);
            if(updateQuesion!=1){
                throw  new CustomizeException(CustomizeCode.QUESTION_NOT_FOUND);
            }
            QuesionUserDto quesionUserDtos=quesionUserSerivce.getByidQuesionUser(quesionId);
            model.addAttribute("quesion",quesionUserDtos);
            //携带数据。让跟新文章之后页面能够保持和上次一样
            List<CommentListDto> commentDtos=quesionUserSerivce.ListBuyQusionId(quesionId,1);//将文章id传递过去
            //将当前文章id，传递到QuesionSerivce中进行和users表的信息关联
            List<Quesions> connectQuesions=quesionUserSerivce.getByConnectQuesions(quesionId);

            QuesionUserDto  quesionUserDtoss=quesionUserSerivce.getByidQuesionUser(quesionId);



            if(user!=null){

                //显示改用户是否关注这个帖子
                Integer bozhu_id=quesionUserSerivce.selectCreator(quesionId);
                Integer checkGuanzhu=attributeMappper.checkGuanZhu(bozhu_id,user.getId());
                //找出当前博主的粉丝
                Integer fensiCount=attributeMappper.selectFenSiCount(bozhu_id);
                if (checkGuanzhu!=0){
                    //已经关注
                    model.addAttribute("isGuanzhu","yes");
                }else{
                    model.addAttribute("isGuanzhu","no");
                }
                model.addAttribute("fensiCount",fensiCount);
                //显示是否收藏博客
                Save save2=saveQuesionMapper.selectUserId(quesionId,user.getId());
                if(save2!=null){
                    //说明已经点赞过
                    model.addAttribute("isSave","save");
                }
                model.addAttribute("userIsLogin","isloging");
                //判断是否第一次发帖
                Integer judge=jMontiorSerivce.checkHasFristFatie(user.getId(),"gengxin");
                //更新等级
                jMontiorSerivce.Upleave(user.getId());
            }else{
                model.addAttribute("userIsLogin","nologing");
            }
            Integer likeCount=saveQuesionMapper.selectLike(quesionId);
            Object object=title;
            Integer insert =jMontiorSerivce.insertDongtai(users.getId(),2,object);//用户id，动态类型，不集体类型

            model.addAttribute("likeCount",likeCount);
            model.addAttribute("quesion",quesionUserDtoss);
            model.addAttribute("commentList",commentDtos);
            model.addAttribute("connectQuesions",connectQuesions);


            return "quesion";
        }

        return "redirect:/";//返回跟目录操作，可以保留上一次的数据
    }
}
